package com.gexin.core;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jfinal.handler.Handler;
import com.jfinal.log.Log;



/**
 * 全局Handler，设置一些通用功能
 */
public class GlobalHandler extends Handler {
	
	@SuppressWarnings("unused")
	private static Log log = Log.getLog(GlobalHandler.class);

	@Override
	public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
		//request.setAttribute("webhost", AppGlobal.Web_Addr);
		//request.setAttribute("basepath", AppGlobal.Base_Path);
		//next.handle(paramString, paramHttpServletRequest, paramHttpServletResponse, paramArrayOfBoolean)
		next.handle(target, request, response, isHandled);
/*
		long starttime = ToolDateTime.getDateByTime();
		reqSysLog.set(Syslog.column_startdate, ToolDateTime.getSqlTimestamp(starttime));//开始时间

		request.setAttribute(ConstantWebContext.reqSysLogKey, reqSysLog);
		
		log.info("设置 web 路径");
		String cxt = ToolWeb.getContextPath(request);
		request.setAttribute(ConstantWebContext.request_cxt, cxt);

		log.info("request 随机分配一个请求id");
		request.setAttribute(ConstantWebContext.request_id, ToolRandoms.getUuid(true));
		
		log.debug("request cookie 处理");
		Map<String, Cookie> cookieMap = ToolWeb.readCookieMap(request);
		request.setAttribute(ConstantWebContext.request_cookieMap, cookieMap);

		log.debug("request param 请求参数处理");
		request.setAttribute(ConstantWebContext.request_paramMap, ToolWeb.getParamMap(request));

		log.debug("request 国际化");
		String localePram = request.getParameter(ConstantWebContext.request_localePram);
		if(null != localePram && !localePram.isEmpty()){
			int maxAge = PropKit.getInt(ConstantInit.config_maxAge_key);
			ToolWeb.addCookie(response,  "", "/", true, ConstantWebContext.cookie_language, localePram, maxAge);
		}else {
			localePram = ToolWeb.getCookieValueByName(request, ConstantWebContext.cookie_language);
			if(null == localePram || localePram.isEmpty()){
				Locale locale = request.getLocale();
				String language = locale.getLanguage();
				localePram = language;
				String country = locale.getCountry();
				if(null != country && !country.isEmpty()){
					localePram += "_" + country;
				}
			}
		}
		localePram = localePram.toLowerCase();
		Map<String, String> i18nMap = I18NPlugin.get(localePram);
		request.setAttribute(ConstantWebContext.request_localePram, localePram);
		request.setAttribute(ConstantWebContext.request_i18nMap, i18nMap);
		
		log.info("设置Header");
		request.setAttribute("decorator", "none");
		response.setHeader("Cache-Control","no-cache"); //HTTP 1.1

		response.setHeader("Pragma","no-cache"); //HTTP 1.0

		response.setDateHeader ("Expires", 0); //prevents caching at the proxy server


		nextHandler.handle(target, request, response, isHandled);*/
		
		
	}
	
	
	
}